package cn.wolfcode.luowowo.article.listener;

import cn.wolfcode.luowowo.api.cache.domain.ScenicRankStatisVO;
import cn.wolfcode.luowowo.api.cache.domain.StrategyStatisVO;
import cn.wolfcode.luowowo.api.cache.prefix.ScenicPrefix;
import cn.wolfcode.luowowo.api.cache.domain.TravelStatisVO;
import cn.wolfcode.luowowo.api.cache.prefix.StrategyStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.prefix.TravelStatisKeyPrefix;
import cn.wolfcode.luowowo.api.cache.service.IRedisService;
import cn.wolfcode.luowowo.api.cache.service.IScenicStatisRedisService;
import cn.wolfcode.luowowo.api.cache.service.IStrategyStatisRedisService;
import cn.wolfcode.luowowo.api.cache.service.ITravelStatisRedisService;
import cn.wolfcode.luowowo.article.domain.StrategyDetail;
import cn.wolfcode.luowowo.article.domain.Travel;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.scenic.domain.Scenic;
import cn.wolfcode.luowowo.scenic.service.IScenicService;
import cn.wolfcode.luowowo.article.service.ITravelService;
import com.alibaba.dubbo.config.annotation.Reference;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * redis数据初始化监听器
 */
@Component
public class RedisDataInitListener implements ApplicationListener<ContextRefreshedEvent>{
    @Reference
    private IStrategyDetailService strategyDetailService;
    @Reference
    private IStrategyStatisRedisService strategyStatisRedisService;

    @Reference
    private ITravelService travelService;
    @Reference
    private ITravelStatisRedisService travelStatisRedisService;



    @Reference
    private IRedisService redisService;
    @Reference
    private IScenicService scenicService;
    @Reference
    private IScenicStatisRedisService scenicStatisRedisService;

    /**
     * 当spring容器启动后马上就会执行的方法
     * @param event
     */
    public void onApplicationEvent(ContextRefreshedEvent event) {
        //获取mybatis中的所有数据
       List<StrategyDetail> list =  strategyDetailService.listAll();
        StrategyStatisVO vo = null;

        // 获取mysql中景点的所有数据
        List<Scenic> scenics = scenicService.getAll();
        ScenicRankStatisVO scenicVO = null;


        //遍历list
        for (StrategyDetail st : list) {
            //redis数据库中存在,就跳过
            if(redisService.exists(StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,st.getId()+"")){
                continue;
            }
            //不存在,添加到redis中
            vo = new StrategyStatisVO();
            vo.setIsabroad(st.isIsabroad());
            vo.setDestId(st.getDest().getId());
            vo.setDestName(st.getDest().getName());
            vo.setTitle(st.getSubTitle());
            vo.setStrategyId(st.getId());
            try {
                BeanUtils.copyProperties(vo,st);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //将vo设置到redis中
            strategyStatisRedisService.setStatisVo(vo);

        }

        // 遍历所有的scenics, 初始化景点统计数据
        for (Scenic sc : scenics) {
            //redis数据库中存在,就跳过
            if(redisService.exists(ScenicPrefix.SCENIC_RANK_STATIS_PREFIX,sc.getId()+"")){
                continue;
            }
            //不存在,添加到redis中
            scenicVO = new ScenicRankStatisVO();
            scenicVO.setScenicId(sc.getId());
            scenicVO.setCommentnum(sc.getCommentnum());
            try {
                BeanUtils.copyProperties(scenicVO,sc);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //将vo设置到redis中
            scenicStatisRedisService.setStatisVo(scenicVO);
        }

        // 景点 TOP5 排行榜初始化
        for (Scenic sc : scenics) {
            if(!redisService.hasZsetValue(ScenicPrefix.SCENIC_TOP5_PREFIX,sc.getDest().getId()+"",
                    ScenicPrefix.SCENIC_RANK_STATIS_PREFIX,":",sc.getId())){
                System.out.println("景点Top排行榜初始化了");
                //阅读数+评论数
                int score = sc.getSynStarnum() + sc.getServiceStarnum() + sc.getSceneryStarnum() + sc.getFeatureStarnum();
                scenicStatisRedisService.addScenicCommendScore(ScenicPrefix.SCENIC_TOP5_PREFIX, sc.getDest().getId() + "", ScenicPrefix.SCENIC_RANK_STATIS_PREFIX, sc.getId(),score);
            }
        }

        // 景点热门排行榜初始化
        for (Scenic sc : scenics) {
            if(!redisService.hasZsetValue(ScenicPrefix.SCENIC_HOTCOMMEND_PREFIX,sc.getDest().getId()+"",
                    ScenicPrefix.SCENIC_RANK_STATIS_PREFIX,":",sc.getDest().getId())){
                System.out.println("景点热门排行榜初始化了");
                //阅读数+评论数
                int score = sc.getCommentnum();
                scenicStatisRedisService.addScenicCommendScore(ScenicPrefix.SCENIC_HOTCOMMEND_PREFIX,sc.getDest().getId() + "", ScenicPrefix.SCENIC_RANK_STATIS_PREFIX, sc.getId(), score);
            }
        }

        //海外+国内初始化 思路:加载的时候,判断zset中是否有已经存在的keyvalue,如果存在,不理,如果不存在,可以添加一个
        for (StrategyDetail detail : list) {
            if(!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,"",
                    StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,":",detail.getId())){
                //收藏数+点赞数
                int score = detail.getFavornum() + detail.getThumbsupnum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_SORT,detail.getId(),score);
            }
        }


       //攻略推荐排序初始化
        for (StrategyDetail detail : list) {
            if(!redisService.hasZsetValue(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,"",
                    StrategyStatisKeyPrefix.STRATEGY_STATIS_VO,":",detail.getId())){
                System.out.println("初始化了");
                //阅读数+评论数
                int score = detail.getViewnum() + detail.getReplynum();
                strategyStatisRedisService.addScore(StrategyStatisKeyPrefix.STRATEGY_STATIS_COMMEND_HOT,detail.getId(),score);
            }
        }


        /**
         * 游记数据初始化到Redis中
         */
        List<Travel> travelAll =  travelService.selectAll();
        TravelStatisVO tvo = null;
        for (Travel t : travelAll) {
            //redis数据库中存在,就跳过
            if(redisService.exists(TravelStatisKeyPrefix.TRAVEL_STATIS_VO,t.getId()+"")){
                continue;
            }
            //不存在,添加到redis中
            tvo = new TravelStatisVO();
            tvo.setDestId(t.getDest().getId());
            tvo.setDestName(t.getDest().getName());
            tvo.setTitle(t.getTitle());
            tvo.setTravelId(t.getId());
            tvo.setViewnum(t.getViewnum());
            tvo.setReplynum(t.getReplynum());
            tvo.setFavornum(t.getFavornum());
            tvo.setSharenum(t.getSharenum());
            tvo.setThumbsupnum(t.getThumbsupnum());

            try {
                BeanUtils.copyProperties(tvo,t);
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            } catch (InvocationTargetException e) {
                e.printStackTrace();
            }
            //将vo设置到redis中
            travelStatisRedisService.setStatisVo(tvo);

        }






    }
}
